package com.example.liunanxin.lnx226ordering.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.liunanxin.lnx226ordering.R;
import com.example.liunanxin.lnx226ordering.beans.FoodBean;
import com.example.liunanxin.lnx226ordering.url.Url;
import com.squareup.picasso.Picasso;

import java.util.List;

public class FoodAdapter extends RecyclerView.Adapter<FoodAdapter.ItemViewHolder>{
    private List<FoodBean> foodBeans;
    FoodAdapter.OnItemClickListener onItemClickListener;

    public interface OnItemClickListener{
        void setItem(View view, int position);
    }

    public void setOnItemClickListener(FoodAdapter.OnItemClickListener onItemClickListener){
        this.onItemClickListener= onItemClickListener;
    }

    //鍒锋柊鍒楄〃鏁版嵁
    public void setData(List<FoodBean> foodBean){
        this.foodBeans=foodBean;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public FoodAdapter.ItemViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, final int i) {
        View view= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_food,viewGroup,false);
        return new FoodAdapter.ItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull FoodAdapter.ItemViewHolder itemViewHolder, int i) {
        FoodBean foodBean=foodBeans.get(i);
        final int position=i;
        Picasso.get()
                .load(Url.USERURL+foodBean.getPic())
                .into(itemViewHolder.img_food);
        itemViewHolder.foodname.setText(foodBean.getFoodname());
        itemViewHolder.foodprice.setText("价格: "+String.valueOf(foodBean.getPrice()));
        itemViewHolder.foodintro.setText(foodBean.getIntro());
        if (onItemClickListener!= null) {
            itemViewHolder.itemview.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onItemClickListener.setItem(view, position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return foodBeans==null?0:foodBeans.size();
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        ImageView img_food;
        TextView foodname,foodprice,foodintro;
        View itemview;

        public ItemViewHolder(@NonNull View view) {
            super(view);
            img_food=view.findViewById(R.id.imageView7);
            foodname=view.findViewById(R.id.textView10);
            foodprice=view.findViewById(R.id.textView11);
            foodintro=view.findViewById(R.id.textView12);
            itemview=view;
        }
    }

}
